package org.lgq.sharding;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;

/**
 * @author DevLGQ
 * @version 1.0
 */
public class SnowSharding implements PreciseShardingAlgorithm<Long> {

    private static final Logger log = LoggerFactory.getLogger(SnowSharding.class);

    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) {
        Long snowId = shardingValue.getValue();
        log.info("snowId: {}", snowId);
        // 对节点个数进行取余
        long mode = snowId % availableTargetNames.size();
        log.info("mode: {}", mode);
        mode = Math.abs(mode);
        String[] array = availableTargetNames.toArray(new String[0]);
        log.info("array: {}", array.toString());
        return array[(int) mode];
    }
}
